<?php

class log extends base {

    private $content;
    private $limitrows = 100;

    public function submenu() {
        $this->submenu = array();
        $this->submenu[0]["name"] = "Show";
        $this->submenu[0]["callback"] = "show";
        $this->submenu[0]["icon"] = "icons/cross.png";
        $this->submenu[0]["rights"] = 0;
        $this->submenu[0]["enabled"] = true;
        $this->_submenu($this->submenu);
    }

    public function delete_all() {
        $r = $this->db->delete_rows("log");
        return $r;
    }

    public function delete($id) {
        if (empty($id))
            return false;
        $r = $this->db->delete_rows("log", "id = {$id}");
        return $r;
    }

    public function show() {
        global $config;
        $this->get_request();
        if (isset($this->requests["Delete_All"]))
            $this->delete_all(); else
        if (isset($this->requests["Delete"]))
            $this->delete($this->requests["id"]);

        $header = array(
            array("title" => "ID", "color" => "green"),
            array("title" => "Logdate", "color" => "green"),
            array("title" => "Section", "color" => "green"),
            array("title" => "Message", "color" => "green"),
        );
        $rows = $this->db->get_rows("log", false, "ORDER by logdate DESC LIMIT {$this->limitrows}");
        foreach ($rows as $row) {
            $row["logdate"] = date("Y-m-d H:i:s", $row["logdate"]);
            $frows[] = $row;
        }
        $button = array("Delete", "Delete All");

        $this->page_manager->start_form("form 1");
        $this->page_manager->style_table($header, $frows, true, $button, 1000, "show");
        return $this->page_manager->web_content;
    }

    private function update_config($config) {
        $requests = $this->requests;
        $page = $requests["page"];
        $submenu = $requests["submenu"];
        unset($requests["page"]);
        unset($requests["submenu"]);
        unset($requests["Change"]);
        foreach ($requests as $request => $value) {
            $config[$submenu][$request] = $value;
            $this->db->delete_rows("settings", "section='{$submenu}' AND name='{$request}'");
            $rows[0] = array("section" => $submenu, "name" => $request, "value" => $value);
            $r = $this->db->insert_rows("settings", $rows);
        }
        return $config;
    }

    public function run() {
        $this->submenu();
        $callback = $this->_run();
        if ($callback)
            $this->content = $this->$callback();
        $this->finish($this->content);
    }

}

?>